package com.book.modules.configure;

import com.book.modules.filter.CurrentUserArgumentResolver;
import com.book.modules.interceptor.CurrentHandlerInterceptor;
import com.book.modules.interceptor.LanguageInterceptor;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * Description：
 *
 * @Author： leo.xiong
 * @CreateDate： 2020/04/21 18:09
 * @Email： leo.xiong@suyun360.com
 * Version：1.0
 */
@Configurable
@Configuration
public class BookWebMvcConfigurer implements WebMvcConfigurer {

    @Bean
    public CurrentHandlerInterceptor currentHandlerInterceptor() {
        return new CurrentHandlerInterceptor();
    }

    @Bean
    public LanguageInterceptor languageInterceptor() {
        return new LanguageInterceptor();
    }

    /**
     * 拦截器
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(currentHandlerInterceptor()).addPathPatterns("/**").excludePathPatterns("/index.html", "/", "/login");
        registry.addInterceptor(languageInterceptor()).addPathPatterns("/**");
    }

    /**
     * 注入参数
     *
     * @param resolvers
     */
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(new CurrentUserArgumentResolver());
    }
}
